<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/8
 * Time: 11:00
 */

namespace app\common\library\ali\sms;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use app\common\model\Config;
use think\response\Json;

class Sms
{
    protected $err;
    protected $PhoneNumbers;
    protected $SignName;
    protected $TemplateCode;
    protected $TemplateParam;

    public function __construct()
    {
        AlibabaCloud::accessKeyClient('','')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
    }

    public function sendSms(){
        try{
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options($this->options())
                ->request();
            return $result->toArray();
        }catch (ClientException $clientException){
            $this->err = $clientException->getMessage() . PHP_EOL;
        }catch (ServerException $serverException){
            $this->err = $serverException->getMessage() . PHP_EOL;
        }
        return json_decode($this->err,true);
    }

    protected function options(){
        $query =  [
            'query'=>[
                'RegionId'=>'cn-hangzhou',
                'PhoneNumbers'=>$this->PhoneNumbers,
                'SignName'=>Config::get('SignName'),
                'TemplateCode'=>$this->TemplateCode,
                'TemplateParam'=>$this->TemplateParam
            ]
        ];
        return $query;
    }

    /**
     * @param mixed $SignName
     * @return Sms
     */
    public function setSignName()
    {
        $this->SignName = Config::get('');
    }

    /**
     * @param mixed $PhoneNumbers
     * @return Sms
     */
    public function setPhoneNumbers($PhoneNumbers)
    {
        $this->PhoneNumbers = $PhoneNumbers;
    }

    /**
     * @param mixed $TemplateCode
     * @return Sms
     */
    public function setTemplateCode($TemplateCode)
    {
        $this->TemplateCode = $TemplateCode;
    }

    /**
     * @param mixed $TemplateParam
     * @param $TemplateParam array
     */
    public function setTemplateParam($TemplateParam)
    {
        $this->TemplateParam = json_encode($TemplateParam);
    }

}